package com.hongyun.tms.web.controller;


import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.annotation.RepeatSubmit;
import com.hongyun.tms.common.annotation.excel.ExcelExport;
import com.hongyun.tms.common.dto.mrate.MRateImportDto;
import com.hongyun.tms.common.dto.mrate.MRateQueryDto;
import com.hongyun.tms.common.dto.mrate.MRateSaveReqDto;
import com.hongyun.tms.common.dto.mrate.MRateTemplateDto;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.vo.MRateVO;
import com.hongyun.tms.domain.AjaxResult;
import com.hongyun.tms.entity.MRate;
import com.hongyun.tms.infra.excel.EasyExcelUtil;
import com.hongyun.tms.infra.excel.listener.MRateExcelListener;
import com.hongyun.tms.service.IMRateService;
import com.hongyun.tms.web.utils.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 拖轮费率 前端控制器
 * </p>
 *
 * @author liPeng
 * @since 2024-06-05
 */
@Api(tags = "拖轮费率管理")
@RestController
@RequestMapping("/mRate")
public class MRateController {

    @Autowired
    private IMRateService rateService;
    /**
     * 分页查询
     *
     * @return page
     */
    @GetMapping("list")
    public AjaxResult<PageResponseDto<MRateVO>> list (MRateQueryDto rateQuery) {
        PageResponseDto<MRateVO> result = rateService.pageList(rateQuery);
        return AjaxResult.success(result);
    }

    /**
     * 根据ID查询
     *
     * @return MRateVO
     */
    @GetMapping("getById")
    public AjaxResult<MRateVO> getById(@RequestParam("id") Long id) {
        MRate mRate = rateService.getById(id);
        return AjaxResult.success(MapperingUtils.copyProperties(mRate, MRateVO.class));
    }

    /**
     * 批量删除
     *
     * @return MRateVO
     */
    @PostMapping("removeBatch")
    @ApiOperation("批量删除拖轮费率")
    @RepeatSubmit
    public AjaxResult removeBatch(@RequestParam(name = "ids") String idList) {
        List<String> ids = Arrays.asList(idList.split(","));
        rateService.removeByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 新增/修改
     *
     * @return MRateVO
     */
    @PostMapping("save")
    @ApiOperation("新增/修改拖轮费率")
    @RepeatSubmit
    public AjaxResult save(@RequestBody @Valid MRateSaveReqDto dto) {
        rateService.saveAndUpdate(dto);
        return AjaxResult.success();
    }
    @ApiOperation("导入费率")
    @PostMapping("/importRate")
    public AjaxResult<Void> importRate(MultipartFile file) throws IOException {
        AjaxResult<Void> validationResult = FileUploadUtil.validateExcelFile(file);
        // 根据code判断校验是否通过
        if (validationResult.getCode() != HttpStatus.OK.value()) {
            return validationResult;
        }

        // 创建 Excel 数据处理监听器
        MRateExcelListener listener = new MRateExcelListener(rateService);

        // 处理 Excel 文件
        try (InputStream inputStream = file.getInputStream()) {
            EasyExcelUtil.syncReadModel(inputStream, listener, MRateImportDto.class, 0, 1);
            return AjaxResult.success();
        }
    }
    @GetMapping("exportTemplate")
    @ExcelExport(value = MRateTemplateDto.class)
    public AjaxResult exportTemplate(HttpServletResponse response) {
        return AjaxResult.success(Collections.emptyList());
    }

    @GetMapping("exportData")
    @ExcelExport(value = MRateVO.class)
    public AjaxResult<List<MRateVO>> exportData(MRateQueryDto query, HttpServletResponse response) {
        List<MRateVO> exportList = rateService.export(query);
        return AjaxResult.success(exportList);
    }
}
